"""
服务端
"""
from socket import  *
from multiprocessing import Process
from dict_db import *

class Handle(Process):
    def __init__(self,conn):
        self.conn = conn
        super().__init__()

    def run(self):
        while True:
            request = self.conn.recv(128).decode()
            tmp = request.split("\t")
            if not request or tmp[0] == "E":
                break



class DictServer:
    def __init__(self,host=" ", port=0):
        self.host = host
        self.port = port
        self.address = (self.host,self.port)
        self.sock = self.create_socket()

    def create_socket(self):
        sock = socket()
        sock.bind(self.address)
        return sock

    def serve_forever(self):
        self.sock.listen(5)
        print("Listen the port %d" % self.port)
        while True:
            conn,addr = self.sock.accept()
            print("Connect from",addr)
            p = Handle(conn)
            p.start()

if __name__ == '__main__':
    p = DictServer(host="0.0.0.0",port=8889)
    p.serve_forever()